package com.tanzeer.deliverytracker.utility;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.tanzeer.deliverytracker.DeliveryListUIUpdaterInterface;
import com.tanzeer.deliverytracker.TrackDetailsUIUpdaterInterface;
import com.tanzeer.deliverytracker.dbhelper.RelsellDBOpenHelper;
import com.tanzeer.deliverytracker.functionality.NavigationSliderCallback;
import com.tanzeer.deliverytracker.models.DelivaryTracks;
import com.tanzeer.deliverytracker.models.RowItem;

public class ApplicationVars {
	private static ApplicationVars sAppVar;
	private boolean mGotDataFromTrackSearchInHome;
	private NavigationSliderCallback mNavigationSliderCallback;
	private boolean mSubscribed;
	private Subscription mSubscriptionObj;
	private boolean detailFragmentOpen;
	private DeliveryListUIUpdaterInterface mDeliveryListCallback;
	private TrackDetailsUIUpdaterInterface mTrackDetailsCallback;
	
	
	private ArrayList<DelivaryTracks> mSavedDataList; // tod od
	
	private  ArrayList<RowItem> mTrackList;
	private ArrayList<RowItem> mCompletedTrackList;
	
	private ArrayList<RowItem> mTempListAfterRefresh;
	
	
	public class Subscription {
		private int mSubscrtionTypeInteger;
		public Subscription(int subscriptionTypeInteger) {
			this.setSubscrtionTypeInteger(subscriptionTypeInteger);
		}
		public int getSubscrtionTypeInteger() {
			return mSubscrtionTypeInteger;
		}
		public void setSubscrtionTypeInteger(int mSubscrtionTypeInteger) {
			this.mSubscrtionTypeInteger = mSubscrtionTypeInteger;
		}
	}

	private ApplicationVars() {
		
	}
	
	public Subscription getSubscriptionObject () {
		return mSubscriptionObj;
	}
    
	public void setSubscriptionObject (Subscription subscriptionObj) {
		this.mSubscriptionObj = subscriptionObj;
	}

	public static ApplicationVars getInstance() {
		if (sAppVar == null) {
			sAppVar = new ApplicationVars();/*
			 * Status will have green background Profile picture If you also put
			 * the delivery time the table will be like original If you put the
			 * border like original one it will not have any confusing. No need
			 * to make remarks place very big, activity can be bigger because it
			 * is now too short to show what it writen.
			 */
		}
		return sAppVar;
	}

	public NavigationSliderCallback getNavigationSliderCallback() {
		return mNavigationSliderCallback;
	}

	public void setNavigationSliderCallback(
			NavigationSliderCallback mNavigationSliderCallback) {
		this.mNavigationSliderCallback = mNavigationSliderCallback;
	}

	public boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (connectivity != null) {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();

			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}
	
	public ArrayList<RowItem> getTrackList() {
		return mTrackList;
	}

	public void setTrackList(ArrayList<RowItem> mTrackList) {
		this.mTrackList = mTrackList;
	}

	public boolean isSubscribed() {
		return mSubscribed;
	}

	public void setSubscribed(boolean mSubscribed) {
		this.mSubscribed = mSubscribed;
	}

	public ArrayList<RowItem> getCompletedTrackList() {
		return mCompletedTrackList;
	}

	public void setCompletedTrackList(ArrayList<RowItem> mCompletedTrackList) {
		this.mCompletedTrackList = mCompletedTrackList;
	}
	
	public ArrayList<RowItem> getTrackNoListToRefresh(Context context,String type,String categoryName, boolean bulkCaller) {
		ArrayList<RowItem> list = new ArrayList<RowItem>();
		RelsellDBOpenHelper rHelper = new RelsellDBOpenHelper(context);
		if(bulkCaller) {
		   rHelper.getItemSummaryList(list, type, categoryName);
		} 
		return list;
	}
	
	public ArrayList<RowItem> getSingleTrackNoList(Context context,String trackId) {
		ArrayList<RowItem> list = new ArrayList<RowItem>();
		RelsellDBOpenHelper rHelper = new RelsellDBOpenHelper(context);
		rHelper.getItemSummaryList(list, trackId);
		return list;
	}
	
	public ArrayList<RowItem> getTrackNoListToRefresh(Context context) {
		ArrayList<RowItem> list = new ArrayList<RowItem>();
		RelsellDBOpenHelper rHelper = new RelsellDBOpenHelper(context);
		rHelper.getItemSummaryListForRefresh(list);
		return list;
	}
	
	public void updateDeliveryListUIAfterManualRefresh(HashMap<String,String> trackIdAndStatus,String returnedRowData) {
		this.mDeliveryListCallback.updateUIForList(trackIdAndStatus);
		if(this.mTrackDetailsCallback != null && this.isDetailFragmentOpen()) {
		   this.mTrackDetailsCallback.updateUIForList(trackIdAndStatus,returnedRowData);
		}
	}

	public DeliveryListUIUpdaterInterface getDeliveryListCallback() {
		return mDeliveryListCallback;
	}

	public void setDeliveryListCallback(DeliveryListUIUpdaterInterface mDeliveryListCallback) {
		this.mDeliveryListCallback = mDeliveryListCallback;
	}

	public ArrayList<RowItem> getTempListAfterRefresh() {
		return mTempListAfterRefresh;
	}

	public void setTempListAfterRefresh(ArrayList<RowItem> mTempListAfterRefresh) {
		this.mTempListAfterRefresh = mTempListAfterRefresh;
	}
	
	public boolean modifiyTempList(RowItem itemToRemove) {
		boolean res = false;
		for (RowItem rowItem : this.getTempListAfterRefresh()){
			 if(rowItem.itemSummary._id == itemToRemove.itemSummary._id) {
				 res = this.getTempListAfterRefresh().remove(itemToRemove);
			 }
		}
		return res;
	}

	public TrackDetailsUIUpdaterInterface getTrackDetailsCallback() {
		return mTrackDetailsCallback;
	}

	public void setTrackDetailsCallback(TrackDetailsUIUpdaterInterface mTrackDetailsCallback) {
		this.mTrackDetailsCallback = mTrackDetailsCallback;
	}

	public boolean isDetailFragmentOpen() {
		return detailFragmentOpen;
	}

	public void setDetailFragmentOpen(boolean detailFragmentOpen) {
		this.detailFragmentOpen = detailFragmentOpen;
	}
}
